package com.siti.kindnessplatform.interceptor;

import com.siti.kindnessplatform.config.EasyCache;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class ViewStatInterceptor extends HandlerInterceptorAdapter {

    public static final String SELECTHOSPITAL="selectHospital";

    public static final String VIEWCOUNT="viewcount";

    @Resource
    EasyCache easyCache;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //接口名字     map<ip,LikeIpVo 对象时间和次数>
        boolean isWatch=request.getRequestURI().contains(SELECTHOSPITAL);
        if(isWatch){
            RedissonClient redisson= easyCache.redisson;
            RAtomicLong num = redisson.getAtomicLong(VIEWCOUNT);
            num.incrementAndGet();
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }
}
